package com.lzy.pattern.composite;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: Menu
 * @Description: 树枝节点（Composite）：定义树枝节点的行为，存储子节点，组合树枝节点和叶子节点形成一个树形结构。
 * @Author: LiuZy
 * @Date: 2022/4/25 08:38
 * @Version: 1.0
 */
public class Menu extends MenuComponent{

    private List<MenuComponent> menuComponentList;

    public Menu(String name, Integer level){
        this.name = name;
        this.level = level;
        menuComponentList = new ArrayList<>();
    }

    @Override
    public void add(MenuComponent component) {
        menuComponentList.add(component);
    }

    @Override
    public void addAll(MenuComponent... component) {
        if(component == null || component.length == 0){
            return;
        }
        menuComponentList.addAll(Arrays.asList(component));
    }

    @Override
    public void remove(MenuComponent component) {
        menuComponentList.remove(component);
    }

    @Override
    public MenuComponent getChild(int index) {
        return menuComponentList.get(index);
    }

    @Override
    public void print() {
        for (Integer i = 1; i < this.level; i++) {
            System.out.print("--");
        }
        System.out.println(this.name);

        for (MenuComponent menuComponent : menuComponentList) {
            menuComponent.print();
        }
    }
}